/*
 *  Copyright 2019-2020 Zheng Jie
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package com.fzhucc.modules.sms;

import com.fzhucc.config.FileProperties;
import com.fzhucc.exception.BadRequestException;
import com.fzhucc.modules.expert.domain.Expert;
import com.fzhucc.modules.expert.domain.dto.*;
import com.fzhucc.modules.expert.repository.ExpertRepository;
import com.fzhucc.modules.expert.service.mapstruct.ExpertMapper;
import com.fzhucc.modules.pms.repository.ProjectNoticeRepository;
import com.fzhucc.modules.system.service.DictDetailService;
import com.fzhucc.modules.system.service.DictService;
import com.fzhucc.modules.system.service.UserService;
import com.fzhucc.modules.system.service.dto.DictDetailDto;
import com.fzhucc.modules.system.service.dto.DictDto;
import com.fzhucc.modules.system.service.dto.DictQueryCriteria;
import com.fzhucc.modules.system.service.dto.UserDto;
import com.fzhucc.utils.*;
import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.common.profile.HttpProfile;
import com.tencentcloudapi.sms.v20190711.SmsClient;
import com.tencentcloudapi.sms.v20190711.models.SendSmsRequest;
import com.tencentcloudapi.sms.v20190711.models.SendSmsResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.sql.Timestamp;
import java.util.*;

/**
 * @author yyg
 * @website https://eladmin.vip
 * @description sendSms
 * @date 2022-12-2
 * **/
@Service
@RequiredArgsConstructor
public class SmsService {

    @Value("${tencent.sms.enable}")
    public boolean enable;

    @Value("${tencent.sms.secretId}")
    public String secretId;

    @Value("${tencent.sms.secretKey}")
    public String secretKey;

    @Value("${tencent.sms.endpoint}")
    public String endpoint;

    @Value("${tencent.sms.region}")
    public String region;

    @Value("${tencent.sms.smsSdkAppId}")
    public String smsSdkAppId;

    @Value("${tencent.sms.signName}")
    public String signName;

    //短信模板 ID:  进入职创项目库
    @Value("${tencent.sms.templateId1}")
    public String templateId1;

    //短信模板 ID:  进入优秀项目库
    @Value("${tencent.sms.templateId2}")
    public String templateId2;

    //短信模板 ID:  进入重点项目库
    @Value("${tencent.sms.templateId3}")
    public String templateId3;

    @Value("${tencent.sms.templatePassFail}")
    public String templatePassFail;

    @Value("${tencent.sms.templateIdForget}")
    public String templateIdForget;

    @Value("${tencent.sms.templateChangeLevel}")
    public String templateChangeLevel;

    @Value("${tencent.sms.templateRandomCode}")
    public String templateRandomCode;

    @Value("${tencent.sms.templatRegistRandomCode}")
    public String templatRegistRandomCode;

    @Value("${tencent.sms.templateInToProject}")
    public String templateInToProject;

    @Value("${tencent.sms.templateAutoApproved}")
    public String templateAutoApproved;

    @Value("${tencent.sms.templateIdSuccessAddActivity}")
    public String templateIdSuccessAddActivity;

    /**
     * 活动报名审核通过通知 模板templateIdSuccessAddActivity
     *
     * @param phone
     * @return
     */
    public boolean sendSuccessAddActivityMsg(String[] phone, String activityName) {
        if (!enable) {
            return false;
        }
        try {
            String[] arrayphone=new String[phone.length];
            for (int i = 0; i < phone.length; i++) {
                String newphone="+86"+phone[i];
                arrayphone[i]=newphone;
            }
            Credential cred = new Credential(secretId, secretKey);
            // 实例化一个http选项，可选的，没有特殊需求可以跳过
            HttpProfile httpProfile = new HttpProfile();
            httpProfile.setEndpoint(endpoint);
            ClientProfile clientProfile = new ClientProfile();
            clientProfile.setHttpProfile(httpProfile);
            SmsClient client = new SmsClient(cred, region, clientProfile);
            SendSmsRequest req = new SendSmsRequest();
            //String[] phoneNumberSet1 = {"12580..."};
            req.setPhoneNumberSet(arrayphone);
            req.setSmsSdkAppid(smsSdkAppId);
            req.setSign(signName);
            req.setTemplateID(templateIdSuccessAddActivity);
            String[] templateParamSet1 = {activityName};
            req.setTemplateParamSet(templateParamSet1);
            SendSmsResponse resp = client.SendSms(req);
            // 输出json格式的字符串回包
            //System.out.println(SendSmsResponse.toJsonString(resp));
            return true;
        } catch (TencentCloudSDKException e) {
            e.printStackTrace();
            return false;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }


    //进入职创项目库
    public boolean SendSmsWithIntoProjectLibrary(String[] phone,String projectName) {
        if (!enable) {
            return false;
        }
        try {
            String[] arrayphone = new String[phone.length];
            for (int i = 0; i < phone.length; i++) {
                String newphone = "+86" + phone[i];
                arrayphone[i] = newphone;
            }
            Credential cred = new Credential(secretId, secretKey);
            HttpProfile httpProfile = new HttpProfile();
            httpProfile.setEndpoint(endpoint);
            ClientProfile clientProfile = new ClientProfile();
            clientProfile.setHttpProfile(httpProfile);
            SmsClient client = new SmsClient(cred, region, clientProfile);
            SendSmsRequest req = new SendSmsRequest();
            req.setPhoneNumberSet(arrayphone);
            req.setSmsSdkAppid(smsSdkAppId);
            req.setSign(signName);
            req.setTemplateID(templateId1);
            String[] templateParamSet1 = {projectName};
            req.setTemplateParamSet(templateParamSet1);
            client.SendSms(req);
            return true;
        } catch (TencentCloudSDKException e) {
            e.printStackTrace();
            return false;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    //进入优秀项目库
    public boolean SendSmsWithIntoExcellentProjectLibrary(String[] phone,String projectName) {
        if (!enable) {
            return false;
        }
        try {
            String[] arrayphone = new String[phone.length];
            for (int i = 0; i < phone.length; i++) {
                String newphone = "+86" + phone[i];
                arrayphone[i] = newphone;
            }
            Credential cred = new Credential(secretId, secretKey);
            HttpProfile httpProfile = new HttpProfile();
            httpProfile.setEndpoint(endpoint);
            ClientProfile clientProfile = new ClientProfile();
            clientProfile.setHttpProfile(httpProfile);
            SmsClient client = new SmsClient(cred, region, clientProfile);
            SendSmsRequest req = new SendSmsRequest();
            req.setPhoneNumberSet(arrayphone);
            req.setSmsSdkAppid(smsSdkAppId);
            req.setSign(signName);
            req.setTemplateID(templateId2);
            String[] templateParamSet1 = {projectName};
            req.setTemplateParamSet(templateParamSet1);
            client.SendSms(req);
            return true;
        } catch (TencentCloudSDKException e) {
            e.printStackTrace();
            return false;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    //进入重点项目库
    public boolean SendSmsWithIntoKeyProjectLibrary(String[] phone,String projectName) {
        if (!enable) {
            return false;
        }
        try {
            String[] arrayphone = new String[phone.length];
            for (int i = 0; i < phone.length; i++) {
                String newphone = "+86" + phone[i];
                arrayphone[i] = newphone;
            }
            Credential cred = new Credential(secretId, secretKey);
            HttpProfile httpProfile = new HttpProfile();
            httpProfile.setEndpoint(endpoint);
            ClientProfile clientProfile = new ClientProfile();
            clientProfile.setHttpProfile(httpProfile);
            SmsClient client = new SmsClient(cred, region, clientProfile);
            SendSmsRequest req = new SendSmsRequest();
            req.setPhoneNumberSet(arrayphone);
            req.setSmsSdkAppid(smsSdkAppId);
            req.setSign(signName);
            req.setTemplateID(templateId3);
            String[] templateParamSet1 = {projectName};
            req.setTemplateParamSet(templateParamSet1);
            client.SendSms(req);
            return true;
        } catch (TencentCloudSDKException e) {
            e.printStackTrace();
            return false;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }


    //项目未通过审核
    public boolean SendSmsIntoLibraryPassFail(String[] phone,String projectName) {
        if (!enable) {
            return false;
        }
        try {
            String[] arrayphone = new String[phone.length];
            for (int i = 0; i < phone.length; i++) {
                String newphone = "+86" + phone[i];
                arrayphone[i] = newphone;
            }
            Credential cred = new Credential(secretId, secretKey);
            HttpProfile httpProfile = new HttpProfile();
            httpProfile.setEndpoint(endpoint);
            ClientProfile clientProfile = new ClientProfile();
            clientProfile.setHttpProfile(httpProfile);
            SmsClient client = new SmsClient(cred, region, clientProfile);
            SendSmsRequest req = new SendSmsRequest();
            req.setPhoneNumberSet(arrayphone);
            req.setSmsSdkAppid(smsSdkAppId);
            req.setSign(signName);
            req.setTemplateID(templatePassFail);
            String[] templateParamSet1 = {projectName};
            req.setTemplateParamSet(templateParamSet1);
            client.SendSms(req);
            return true;
        } catch (TencentCloudSDKException e) {
            e.printStackTrace();
            return false;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    //项目等级变更
    public boolean SendSmsIntoLibraryChangeLevel(String[] phone,String projectName,String levelName1,String levelName2) {
        if (!enable) {
            return false;
        }
        try {
            String[] arrayphone = new String[phone.length];
            for (int i = 0; i < phone.length; i++) {
                String newphone = "+86" + phone[i];
                arrayphone[i] = newphone;
            }
            Credential cred = new Credential(secretId, secretKey);
            HttpProfile httpProfile = new HttpProfile();
            httpProfile.setEndpoint(endpoint);
            ClientProfile clientProfile = new ClientProfile();
            clientProfile.setHttpProfile(httpProfile);
            SmsClient client = new SmsClient(cred, region, clientProfile);
            SendSmsRequest req = new SendSmsRequest();
            req.setPhoneNumberSet(arrayphone);
            req.setSmsSdkAppid(smsSdkAppId);
            req.setSign(signName);
            req.setTemplateID(templateChangeLevel);
            String[] templateParamSet1 = {projectName,levelName1,levelName2};
            req.setTemplateParamSet(templateParamSet1);
            client.SendSms(req);
            return true;
        } catch (TencentCloudSDKException e) {
            e.printStackTrace();
            return false;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    //项目入库时发送
    public boolean SendSmsIntoProject(String[] phone,String projectName) {
        if (!enable) {
            return false;
        }
        try {
            String[] arrayphone = new String[phone.length];
            for (int i = 0; i < phone.length; i++) {
                String newphone = "+86" + phone[i];
                arrayphone[i] = newphone;
            }
            Credential cred = new Credential(secretId, secretKey);
            HttpProfile httpProfile = new HttpProfile();
            httpProfile.setEndpoint(endpoint);
            ClientProfile clientProfile = new ClientProfile();
            clientProfile.setHttpProfile(httpProfile);
            SmsClient client = new SmsClient(cred, region, clientProfile);
            SendSmsRequest req = new SendSmsRequest();
            req.setPhoneNumberSet(arrayphone);
            req.setSmsSdkAppid(smsSdkAppId);
            req.setSign(signName);
            req.setTemplateID(templateInToProject);
            String[] templateParamSet1 = {projectName};
            req.setTemplateParamSet(templateParamSet1);
            client.SendSms(req);
            return true;
        } catch (TencentCloudSDKException e) {
            e.printStackTrace();
            return false;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    //自动审核发送
    public boolean SendSmsAutoApproved(String[] phone,String projectName) {
        if (!enable) {
            return false;
        }
        try {
            String[] arrayphone = new String[phone.length];
            for (int i = 0; i < phone.length; i++) {
                String newphone = "+86" + phone[i];
                arrayphone[i] = newphone;
            }
            Credential cred = new Credential(secretId, secretKey);
            HttpProfile httpProfile = new HttpProfile();
            httpProfile.setEndpoint(endpoint);
            ClientProfile clientProfile = new ClientProfile();
            clientProfile.setHttpProfile(httpProfile);
            SmsClient client = new SmsClient(cred, region, clientProfile);
            SendSmsRequest req = new SendSmsRequest();
            req.setPhoneNumberSet(arrayphone);
            req.setSmsSdkAppid(smsSdkAppId);
            req.setSign(signName);
            req.setTemplateID(templateAutoApproved);
            String[] templateParamSet1 = {projectName};
            req.setTemplateParamSet(templateParamSet1);
            client.SendSms(req);
            return true;
        } catch (TencentCloudSDKException e) {
            e.printStackTrace();
            return false;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }


    //发送六位验证码
    public boolean SendRandomSms(String[] phone,String randomCode,String expiresTime) {
        if (!enable) {
            return false;
        }
        try {
            String[] arrayphone = new String[phone.length];
            for (int i = 0; i < phone.length; i++) {
                String newphone = "+86" + phone[i];
                arrayphone[i] = newphone;
            }
            Credential cred = new Credential(secretId, secretKey);
            HttpProfile httpProfile = new HttpProfile();
            httpProfile.setEndpoint(endpoint);
            ClientProfile clientProfile = new ClientProfile();
            clientProfile.setHttpProfile(httpProfile);
            SmsClient client = new SmsClient(cred, region, clientProfile);
            SendSmsRequest req = new SendSmsRequest();
            req.setPhoneNumberSet(arrayphone);
            req.setSmsSdkAppid(smsSdkAppId);
            req.setSign(signName);
            req.setTemplateID(templateRandomCode);
            String[] templateParamSet1 = {randomCode,expiresTime};
            req.setTemplateParamSet(templateParamSet1);
            client.SendSms(req);
            return true;
        } catch (TencentCloudSDKException e) {
            e.printStackTrace();
            return false;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    //发送注册手机验证码
    public boolean sendtemplatRegistRandomCode(String[] phone,String randomCode,String expiresTime) {
        if (!enable) {
            return false;
        }
        try {
            String[] arrayphone = new String[phone.length];
            for (int i = 0; i < phone.length; i++) {
                String newphone = "+86" + phone[i];
                arrayphone[i] = newphone;
            }
            Credential cred = new Credential(secretId, secretKey);
            HttpProfile httpProfile = new HttpProfile();
            httpProfile.setEndpoint(endpoint);
            ClientProfile clientProfile = new ClientProfile();
            clientProfile.setHttpProfile(httpProfile);
            SmsClient client = new SmsClient(cred, region, clientProfile);
            SendSmsRequest req = new SendSmsRequest();
            req.setPhoneNumberSet(arrayphone);
            req.setSmsSdkAppid(smsSdkAppId);
            req.setSign(signName);
            req.setTemplateID(templatRegistRandomCode);
            String[] templateParamSet1 = {randomCode,expiresTime};
            req.setTemplateParamSet(templateParamSet1);
            client.SendSms(req);
            return true;
        } catch (TencentCloudSDKException e) {
            e.printStackTrace();
            return false;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    //发送注册手机验证码
    public boolean sendtemplatForget(String[] phone,String randomCode) {
        if (!enable) {
            return false;
        }
        try {
            String[] arrayphone = new String[phone.length];
            for (int i = 0; i < phone.length; i++) {
                String newphone = "+86" + phone[i];
                arrayphone[i] = newphone;
            }
            Credential cred = new Credential(secretId, secretKey);
            HttpProfile httpProfile = new HttpProfile();
            httpProfile.setEndpoint(endpoint);
            ClientProfile clientProfile = new ClientProfile();
            clientProfile.setHttpProfile(httpProfile);
            SmsClient client = new SmsClient(cred, region, clientProfile);
            SendSmsRequest req = new SendSmsRequest();
            req.setPhoneNumberSet(arrayphone);
            req.setSmsSdkAppid(smsSdkAppId);
            req.setSign(signName);
            req.setTemplateID(templateIdForget);
            String[] templateParamSet1 = {randomCode};
            req.setTemplateParamSet(templateParamSet1);
            client.SendSms(req);
            return true;
        } catch (TencentCloudSDKException e) {
            e.printStackTrace();
            return false;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
}